package cn.lijigou.shopping;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.zip.Inflater;

import cn.lijigou.shopping.HotPlaceActivity.ExpandInfoAdapter;
import cn.lijigou.shopping.domain.CityTag;
import cn.lijigou.shopping.domain.MResult;
import cn.lijigou.shopping.service.MSystem;
import cn.lijigou.shopping.util.GetSingleThreadPool;
import cn.lijigou.shopping.views.WidgetScrollView;
import cn.lijigou.shopping.views.WidgetScrollViewRight;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class DialogWidgetActivity extends Activity implements OnClickListener {
	private WidgetScrollView scrollView1;
	public static WidgetScrollViewRight scrollView2;
	private LinearLayout parent1;
	public static LinearLayout parent2;
	private TextView tv = null;
	private View footerView;
	public static CityTag ct = null;
	private String city = "";
	private ProgressDialog mProgress = null;
	private  List<CityTag> tags = new ArrayList<CityTag>();
	public static  LayoutInflater layoutInflater;
	private ExecutorService executorService = GetSingleThreadPool.getInstance();
	
	public Handler resultHander = new Handler(){
		
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				hideProgress();
				for(int i=0;i<ct.getTags().size();i++){
		        	 View View = layoutInflater.inflate(R.layout.widget_item, null);
		        	 TextView textView=(TextView)View.findViewById(R.id.widget_text);
		        	 parent1.addView(View);
		        	 textView.setText(ct.getTags().get(i).getTag_name());
		        }
				for(int i=0;i<2;i++){
			       	 View View = layoutInflater.inflate(R.layout.widget_item, null);
			       	 TextView textView=(TextView)View.findViewById(R.id.widget_text);
			       	 parent1.addView(View);
			       }
				
				
				
		        for(int i=0;i<ct.getTags().get(0).getTags().size();i++){
		       	 View View = layoutInflater.inflate(R.layout.widget_item, null);
		       	 TextView textView=(TextView)View.findViewById(R.id.widget_text);
		       	 parent2.addView(View);
		       	 textView.setText(ct.getTags().get(0).getTags().get(i).getTag_name());
		       }
				
		        for(int i=0;i<2;i++){
			       	 View View = layoutInflater.inflate(R.layout.widget_item, null);
			       	 TextView textView=(TextView)View.findViewById(R.id.widget_text);
			       	 parent2.addView(View);
			       }
				
				
				
				break;

			default:
				break;
			}
		
		  }
		};
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.dialogwidget);
        WidgetScrollView.shopindex=0;
        WidgetScrollViewRight.shopindex=0;
        
        scrollView1=(WidgetScrollView)this.findViewById(R.id.scrollView1);
        scrollView2=(WidgetScrollViewRight)this.findViewById(R.id.scrollView2);
        parent1=(LinearLayout)this.findViewById(R.id.parent1);
        parent2=(LinearLayout)this.findViewById(R.id.parent2);
        
        this.findViewById(R.id.widget_ok).setOnClickListener(this);
        this.findViewById(R.id.widget_cancel).setOnClickListener(this);
        
        layoutInflater=LayoutInflater.from(this);
        for(int i=0;i<2;i++){
       	 View View = layoutInflater.inflate(R.layout.widget_item, null);
       	 TextView textView=(TextView)View.findViewById(R.id.widget_text);
       	 parent1.addView(View);
       }
     
      
        getAreaInfo();
    }

    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if (keyCode == KeyEvent.KEYCODE_BACK) {
    		timer.cancel();
    		hideProgress();
    		DialogWidgetActivity.this.finish();
		
    		return true;
	    }else{
	    	return super.onKeyDown(keyCode, event);
	    }
    }
	
	

	protected void showProgress() {
		this.runOnUiThread(new Runnable() {
			public void run() {	
				if(mProgress==null){
					mProgress = new ProgressDialog(DialogWidgetActivity.this);
					mProgress.requestWindowFeature(Window.FEATURE_NO_TITLE);
					mProgress.setMessage("商圈加载中,请稍后...");
					
				}
				mProgress.show();
				
				
			}
		});
	}

	/**
	 * 取消等待框
	 */
	protected void hideProgress() {
		
		this.runOnUiThread(new Runnable() {
			public void run() {
				if (mProgress != null) {
					mProgress.dismiss();
				}
			}
		});
	}
    
    public static void Notify(){
//    	WidgetScrollView.shopindex;
    	if(ct!=null){
	    	if(ct.getTags().size()>0){
	    		parent2.removeAllViews();
			   for(int i=0;i<2;i++){
		          	 View View = layoutInflater.inflate(R.layout.widget_item, null);
		          	 TextView textView=(TextView)View.findViewById(R.id.widget_text);
		          	 parent2.addView(View);
		          }
			   
			   scrollView2.post(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub
						scrollView2.scrollTo(0, 0);
					}
				});
			   WidgetScrollViewRight.shopindex=0;
		    	int total=WidgetScrollView.shopindex;
		    	for(int i=0;i<ct.getTags().get(total).getTags().size();i++){
			       	 View View = layoutInflater.inflate(R.layout.widget_item, null);
			       	 TextView textView=(TextView)View.findViewById(R.id.widget_text);
			       	 parent2.addView(View);
			       	 textView.setText(ct.getTags().get(total).getTags().get(i).getTag_name());
			       }
		    	for(int i=0;i<2;i++){
			       	 View View = layoutInflater.inflate(R.layout.widget_item, null);
			       	 TextView textView=(TextView)View.findViewById(R.id.widget_text);
			       	 parent2.addView(View);
			       }
	    	}
    	}
    }
    
    
	TimerTask task = null;
	Timer timer = null;
	private void getAreaInfo(){
		if(task==null){
			task = new TimerTask() { 
				public void run() { 
					showProgress();
					if(MSystem.mLocation!=null){
						if(getNetConnectState(DialogWidgetActivity.this)){
							
							timer.cancel();
							executorService.execute(new Thread() {
								public void run() {
									ct = new CityTag();
									MResult mResult = MSystem.setHotPlace(ct);
									if (mResult != null) {
										if (mResult.isSuccess()) {
											if (ct != null) {
												if (tv != null) {
													resultHander.sendEmptyMessage(2);
												}
												city = ct.getTag_name();
												tags = ct.getTags();
												resultHander.sendEmptyMessage(1);
											} else {
												resultHander.sendEmptyMessage(0);
											}
										} else {
											resultHander.sendEmptyMessage(3);
										}
									} else {
										resultHander.sendEmptyMessage(4);
									}
								}
							});
						}else{
							
						}
					 }
				} 
			}; 
		}
		if( timer==null){
			timer = new Timer(); 
			timer.schedule(task, 0,7000);
		}
	}
	 
	public boolean getNetConnectState(Context context) {
		boolean netConnect = false;
		ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		NetworkInfo infoM = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (info.isConnected() || infoM.isConnected()) {
			netConnect = true;
		}
		return netConnect;
	}



	
	public void onClick(View v) {
		MSystem.pv+=1;
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.widget_ok:
			 if(WidgetScrollView.shopindex==0){
				 MSystem.area ="";
				 MSystem.areaName ="";
			 }else{
				
				 MSystem.area = tags.get(WidgetScrollView.shopindex)
					.getTags().get(WidgetScrollViewRight.shopindex).getId()+"";
					if(WidgetScrollViewRight.shopindex==0){
						MSystem.areaName = tags.get(WidgetScrollView.shopindex)
						.getTag_name();
					}else{
						MSystem.areaName = tags.get(WidgetScrollView.shopindex)
						.getTags().get(WidgetScrollViewRight.shopindex).getTag_name();
					}
			 }
			
			 Intent intent_s = new Intent(DialogWidgetActivity.this, ShoppingsActivity.class);
			 DialogWidgetActivity.this.setResult(1, intent_s);
			 DialogWidgetActivity.this.finish();
			break;
		case R.id.widget_cancel:
			DialogWidgetActivity.this.finish();
			break;		

		default:
			break;
		}
		if(timer!=null){
			timer.cancel();
		}
		
		hideProgress();
	}
		
}
